package com.fjph.myapplication.ui

import android.content.Intent
import android.os.Bundle
import android.support.v4.app.FragmentActivity
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.widget.Toast


import com.fjph.myapplication.R
import com.fjph.myapplication.model.StoriesBean
import com.fjph.myapplication.model.ZhihuBean
import com.fjph.myapplication.ui.adapter.MyAdapter
import com.fjph.myapplication.ui.adapter.ZhihuAdapter
import com.google.gson.Gson
import com.jcodecraeer.xrecyclerview.ProgressStyle
import com.jcodecraeer.xrecyclerview.XRecyclerView
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.lzy.okgo.request.base.Request
import com.lzy.okgo.utils.HttpUtils
import kotlinx.android.synthetic.main.activity_main2.*

import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.ArrayList
import java.util.Calendar
import java.util.Date

class Main2Activity : FragmentActivity() {

    var mLayoutManager: LinearLayoutManager? = null;
    var mAdapter: ZhihuAdapter? = null;
    var list = ArrayList<StoriesBean>()
    var currTime = 0
    var temp: Long? = java.lang.Long.valueOf(0)

    val instance by lazy { this }

    override fun onCreate(savedInstanceState: Bundle?) {
        setContentView(R.layout.activity_main2)
        super.onCreate(savedInstanceState)
        initView()
    }

    private fun initView() {
        initRecyclerView()
    }

    private fun initRecyclerView() {
        //创建并设置Adapter
        mAdapter = ZhihuAdapter(list, this)
        my_recycler.setArrowImageView(R.mipmap.iconfont_downgrey);

        mLayoutManager = LinearLayoutManager(this)
        mLayoutManager!!.orientation = LinearLayoutManager.VERTICAL
        my_recycler!!.layoutManager = mLayoutManager
        my_recycler!!.adapter = mAdapter

        my_recycler.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        my_recycler.setLoadingMoreProgressStyle(ProgressStyle.SquareSpin)

        my_recycler!!.setLoadingListener(object : XRecyclerView.LoadingListener {
            override fun onRefresh() {
                currTime = todayDate
                getData(currTime, true)
            }

            override fun onLoadMore() {
                if (currTime > 0 && currTime > 20130520) {
                    currTime = date
                    getData(currTime, false)
                }
            }
        })
        my_recycler!!.refresh();

    }


    private fun getData(time: Int, isrefresh: Boolean) {
        var url = "http://news-at.zhihu.com/api/4/news/before/$time"

        if (isrefresh) {
            url = "http://news-at.zhihu.com/api/4/news/latest"
        }

        OkGo.get<String>(url).execute(object : StringCallback() {

            override fun onStart(request: Request<String, out Request<Any, Request<*, *>>>?) {
                super.onStart(request)
            }

            override fun onFinish() {
                super.onFinish()
            }

            override fun onError(response: Response<String>?) {
                super.onError(response)
                Toast.makeText(instance, response!!.body(), Toast.LENGTH_SHORT).show()
            }

            override fun onSuccess(response: Response<String>?) {

                val data = (response?.body() ?: "");
                val model = Gson().fromJson(data, ZhihuBean::class.javaObjectType)
                if (isrefresh) {
                    list.clear()
                }
                list.addAll(model.stories!!)
                mAdapter!!.notifyDataSetChanged()
                if (isrefresh) {
                    my_recycler!!.refreshComplete()
                } else {
                    my_recycler!!.loadMoreComplete()
                }
            }

        });


    }


    /**
     * 获取今天年月日
     *
     * @return
     */
    val todayDate: Int
        get() {
            val date = Date()
            val df = SimpleDateFormat("yyyyMMdd")
            temp = date.time
            return Integer.parseInt(df.format(temp))
        }


    /**
     * 获取前一天
     *
     * @return
     */
    val date: Int
        get() {

            if (currTime <= 0 && temp!! <= 0) {
                return 0
            }
            val calendar = Calendar.getInstance()
            calendar.time = Date(temp!!)
            calendar.add(Calendar.DAY_OF_YEAR, -1)
            val df = SimpleDateFormat("yyyyMMdd")
            temp = calendar.time.time
            return Integer.parseInt(df.format(temp))
        }


}
